<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <input type="text" placeholder="输入姓名" v-model="keyWord">
        <button @click="order = 'asc'">年龄升序</button>
        <button @click="order = 'desc'">年龄降序</button>
        <button @click="order = 'starsOrder'">原顺序</button>
        <ul>
            <li v-for="star in result">
                {{star.name}} - {{star.age}} - {{star.sex}}
            </li>
        </ul>
    </div>
    <script src="../vuejs/vue.js"></script>
    <script>
        const vm = new Vue({
            el : '#app',
            data() {
                return {
                    stars: [{
                        id : 001,
                        name : "周冬雨",
                        age : 25,
                        sex : "女"
                    },{
                        id : 002,
                        name : "马冬梅",
                        age : 24,
                        sex : "女"
                    },{
                        id : 003,
                        name : "周杰伦",
                        age : 23,
                        sex : "男"
                    },{
                        id : 004,
                        name : "周杰",
                        age : 40,
                        sex : "男"
                    }],
                    order : 'starsOrder',
                    keyWord: ''
                }
            },

            computed : {
                result : {
                    get(){
                        // alert(this.order);
                        const filterResult = this.stars.filter(item => {
                            return item.name.includes(this.keyWord)
                        })
                        if(this.order === 'asc'){
                            return filterResult.sort(function(a,b){
                            return a.age-b.age
                        })
                        }else if(this.order === 'desc'){
                            return filterResult.sort(function(a,b){
                            return b.age-a.age
                        })
                        }else {
                            return filterResult
                        }
                    }
                }
            }
        })
    </script>
</body>
</html>